<?php
namespace app\common\dao;

use app\common\model\AdminModel;
use app\common\trail\DaoTrail;
use app\common\utils\Crypter;
use think\facade\Config;

/**
 * 管理员
 */
class AdminDao extends AdminModel
{
    use DaoTrail;

    /**
     * 模型
     */
    protected $model = AdminModel::class;

    /**
     * 生成管理员签名
     *
     * @param integer $admin_id 管理员ID
     * @return string
     */
    public function getAdminAuth($admin_id)
    {
        if (empty($admin_id)) {
            return "";
        }

        // 获取帐号资料
        $admin = $this->where([
            ["id", "=", $admin_id],
        ])->find();
        if (empty($admin)) {
            return "";
        }
        $admin = $admin->toArray();

        // 脱敏
        unset($admin['password']);

        // 加密JSON
        $opensslkey = trim(Config::get("auth.opensslkey"));
        $auth       = (new Crypter($opensslkey))->encrypt(json_encode($admin, JSON_UNESCAPED_UNICODE));

        return $auth;
    }
}
